package com.example.t1.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class Crop_Canvas extends ImageView {

    private final static int PRESS_LB = 0;// 表示左下角矩形框
    private final static int PRESS_LT = 1;// 表示左上角矩形框
    private final static int PRESS_RB = 2;// 表示右下角矩形框
    private final static int PRESS_RT = 3;// 表示右上角矩形框

    private Bitmap bitMap = null; // 原始图片
    /**
     * 经过比例转换后的裁剪区域
     */
    // private RectF src = null;
    /**
     * 图片显示区域，也就是drawBitmap函数中的目标dst
     */
    private RectF dst = null;
    /**
     * 选择区域
     */
    private RectF ChooseArea = null;
    private Paint mPaint = null, rectPaint = null; // 画笔
    private Matrix matrix = null; // 矩阵

    private float mx = 0; // 存储触笔移动时，之前所在的触笔的x坐标
    private float my = 0; // 存储触笔移动时，之前所在的触笔的y坐标
    private boolean touchFlag = false; // 触笔是否在屏幕之上
    private boolean cutFlag = false; // 是否点击了menu上的裁剪按钮
    private int recFlag = -1; // 用来存储触笔点击了哪个小矩形框（改变选择区域大小的小矩形框）
    private boolean firstFlag = false;

    private RectF recLT = null; // 左上角的小矩形框
    private RectF recRT = null; // 右上角的小矩形框
    private RectF recLB = null; // 左下角的小矩形框
    private RectF recRB = null; // 右下角的小矩形框
    private static final int LEFT_AREA_ALPHA = 50 * 255 / 100;
    private RectF leftRectL = null;
    private RectF leftRectR = null;
    private RectF leftRectT = null;
    private RectF leftRectB = null;
    private Paint leftAreaPaint = null;

    private int myWidth;
    private int myHeight;

    private int left, right, top, bottom;

    private boolean scaleFlag;
    private Bitmap bitmap;
    private Context context;

    public Crop_Canvas(Context context, AttributeSet attrs) {
	super(context, attrs);
	this.context = context;
	this.init();
    }

    public Crop_Canvas(Context context) {
	super(context);
	this.context = context;
	this.init();
    }

    public void setBitmap(Bitmap bitmap, int myWidth, int myHeigh) {
	this.bitmap = bitmap;
	this.myWidth = myWidth;
	this.myHeight = myHeigh;
    }

    private void init() {
	Log.d("tag", "构造方法");
	cutFlag = true;
	recLT = new RectF();
	recLB = new RectF();
	recRT = new RectF();
	recRB = new RectF();
	dst = new RectF();
	mPaint = new Paint();
	mPaint.setColor(0xff33B5E5);
	mPaint.setStyle(Paint.Style.STROKE); // 将画笔的风格改为空心
	mPaint.setStrokeWidth(5);// 设置宽度
	// 四个方块的画笔
	rectPaint = new Paint();
	rectPaint.setStyle(Paint.Style.FILL);
	rectPaint.setColor(0xff33B5E5);
	ChooseArea = new RectF();
	this.setPressRecLoc();
	// src = null;
	firstFlag = true;

	// 选择框之外的灰色区域，分成四个矩形框

	leftAreaPaint = new Paint();
	leftAreaPaint.setStyle(Paint.Style.FILL);
	leftAreaPaint.setAlpha(Crop_Canvas.LEFT_AREA_ALPHA);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
	    int bottom) {
	super.onLayout(changed, left, top, right, bottom);
	setMyBitmap(getWidth(), getHeight());
    }

    private void setMyBitmap(int screenWidth, int screenHeight) {
	Log.d("tag", "setBitmap");
	if (bitmap == null) {
	    ((Activity) this.context).finish();
	    return;
	}
	// bitmap 转换成 BitmapDrawable 宽高会缩小一半
	BitmapDrawable bd = new BitmapDrawable(bitmap);
	// int bitWidth = bitmap.getWidth();
	// int bitHeight = bitmap.getHeight();
	ChooseArea = new RectF(screenWidth / 2 - myWidth / 2, screenHeight / 2
		- myHeight / 2, screenWidth / 2 + myWidth / 2, screenHeight / 2
		+ myHeight / 2);

	this.bitMap = bitmap.copy(Config.ARGB_8888, true);

	this.setImageBitmap(bitMap);
	leftRectB = new RectF();
	leftRectL = new RectF();
	leftRectR = new RectF();
	leftRectT = new RectF();
	dst.set(0, 0, screenWidth, screenHeight);
	this.setPressRecLoc();
    }

    // // 裁剪出选择区域里的图片
    // // 之前要做比例转换，因为，你选择的区域是针对比例转换后的图片
    // // 所以要使用ChooseArea的数值做比例转换，然后，使用这些数值重新设置rec的大小
    public Bitmap getSubsetBitmap() {
	float ratioWidth = bitMap.getWidth() / (float) (dst.right - dst.left);
	float ratioHeight = bitMap.getHeight() / (float) (dst.bottom - dst.top);
	int left = (int) ((ChooseArea.left - dst.left) * ratioWidth);
	int right = (int) (left + (ChooseArea.right - ChooseArea.left)
		* ratioWidth);
	int top = (int) ((ChooseArea.top - dst.top) * ratioHeight);
	int bottom = (int) (top + (ChooseArea.bottom - ChooseArea.top)
		* ratioHeight);
	// src = new RectF(left, top, right, bottom);
	firstFlag = true;
	set_LeftArea_Alpha();
	// getDrawingCache();
	//小于设置的宽高不剪裁直接返回 
	if (bitMap.getWidth() > myWidth && bitMap.getHeight() > myHeight) {
	    return Bitmap.createBitmap(bitMap, left, top, right - left, bottom
		    - top);
	} else {
	    return bitMap;
	}

    }

    /**
     * 
     * 描述： 裁剪出选择区域里的图片之前要做比例转换，因为，你选择的区域是针对比例转换后的图片
     * 所以要使用ChooseArea的数值做比例转换，然后，使用这些数值重新设置rec的大小
     * 
     * @return
     * @throws
     * @date：2013-9-30 下午4:19:11
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    // public Bitmap getSubsetBitmap() {
    // float ratioWidth = bitMap.getWidth() / (float) (dst.right -
    // dst.left);
    // float ratioHeight = bitMap.getHeight() / (float) (dst.bottom -
    // dst.top);
    // int left = (int) ((ChooseArea.left - dst.left) * ratioWidth);
    // int right = (int) (left + (ChooseArea.right - ChooseArea.left)
    // * ratioWidth);
    // int top = (int) ((ChooseArea.top - dst.top) * ratioHeight);
    // int bottom = (int) (top + (ChooseArea.bottom - ChooseArea.top)
    // * ratioHeight);
    // // src = new RectF(left, top, right, bottom);
    // firstFlag = true;
    // set_LeftArea_Alpha();
    // getDrawingCache();
    // return Bitmap.createBitmap(convertViewToBitmap(),
    // (int) ChooseArea.left, (int) ChooseArea.top,
    // (int) ChooseArea.right - (int) ChooseArea.left,
    // (int) ChooseArea.bottom - (int) ChooseArea.top);
    // }

    // 获得ChooseArea对象
    public RectF getChooseArea() {
	return ChooseArea;
    }

    // 移动选择区域，选择区域是不能从图片区域里移出去的
    public void moveChooseArea(float move_x, float move_y) {
	if (ChooseArea.left + move_x >= dst.left
		&& ChooseArea.right + move_x <= dst.right
		&& ChooseArea.top + move_y >= dst.top
		&& ChooseArea.bottom + move_y <= dst.bottom) {
	    ChooseArea.set(ChooseArea.left + move_x, ChooseArea.top + move_y,
		    ChooseArea.right + move_x, ChooseArea.bottom + move_y);
	} else {
	    if (ChooseArea.left + move_x < dst.left) {
		ChooseArea.set(dst.left, ChooseArea.top, ChooseArea.right
			+ dst.left - ChooseArea.left, ChooseArea.bottom);
	    }
	    if (ChooseArea.right + move_x > dst.right) {
		ChooseArea.set(ChooseArea.left + dst.right - ChooseArea.right,
			ChooseArea.top, dst.right, ChooseArea.bottom);
	    }

	    if (ChooseArea.top + move_y < dst.top) {
		ChooseArea.set(ChooseArea.left, dst.top, ChooseArea.right,
			ChooseArea.bottom + dst.top - ChooseArea.top);
	    }

	    if (ChooseArea.bottom + move_y > dst.bottom) {
		ChooseArea.set(ChooseArea.left, ChooseArea.top + dst.bottom
			- ChooseArea.bottom, ChooseArea.right, dst.bottom);
	    }
	}
	this.setPressRecLoc();
	// mPaint.setColor(Color.GREEN);
	this.invalidate();
    }

    public boolean onTouchEvent(MotionEvent event) {
	// mPaint.setColor(Color.RED);
	// switch (event.getAction()) {
	// case MotionEvent.ACTION_DOWN:
	// if (cutFlag) {
	// // System.out.println(event.getX() + "," + event.getY());
	// // 判断触笔是否在裁剪区域内，也就是ChooseArea内
	// // 如果是，整个区域随着鼠标移动而移动
	// mx = (int) event.getX();
	// my = (int) event.getY();
	// if (this.judgeLocation(mx, my)) {
	// touchFlag = true;
	// // mPaint.setColor(Color.GREEN);
	// this.invalidate();
	// return true;
	// } else {
	// // 不在裁剪区域内，就判断触笔是否在改变区域大小的小矩形框之内
	// if (this.findPresseddst((int) event.getX(),
	// (int) event.getY())) {
	// touchFlag = true;
	// // mPaint.setColor(Color.RED);
	// return true;
	// }
	// }
	// }
	// case MotionEvent.ACTION_MOVE:
	// if (touchFlag) {
	// // 判断是否点击了哪个个小矩形框
	// if (scaleFlag) {
	// if (this.isOutOfAreaScale(event.getX(), event.getY())) {
	// return true;
	// }
	// } else {
	// if (this.isOutOfArea((int) event.getX(), (int) event.getY())) {
	// return true;
	// }
	// }
	//
	// // 如果选择区域大小跟图像大小一样时，就不能移动
	// if (ChooseArea.left == dst.left && ChooseArea.top == dst.top
	// && ChooseArea.right == dst.right
	// && ChooseArea.bottom == dst.bottom) {
	// } else {
	// this.moveChooseArea(event.getX() - mx, event.getY() - my);
	// mx = (int) event.getX();
	// my = (int) event.getY();
	// }
	// }
	// break;
	// case MotionEvent.ACTION_UP:
	// // 触笔移出屏幕时，将一些变量设回初值
	// if (event.getAction() == MotionEvent.ACTION_UP) {
	// recFlag = -1;
	// this.invalidate();
	// touchFlag = false;
	// }
	// break;
	//
	// default:
	// break;
	// }
	// 点击了裁剪按钮之后才会执行以下事件
	if (event.getAction() == MotionEvent.ACTION_DOWN && cutFlag) {
	    // System.out.println(event.getX() + "," + event.getY());
	    // 判断触笔是否在裁剪区域内，也就是ChooseArea内
	    // 如果是，整个区域随着鼠标移动而移动
	    mx = (int) event.getX();
	    my = (int) event.getY();
	    if (this.judgeLocation(mx, my)) {
		touchFlag = true;
		// mPaint.setColor(Color.GREEN);
		this.invalidate();
		return true;
	    } else {
		// 不在裁剪区域内，就判断触笔是否在改变区域大小的小矩形框之内
		if (this.findPresseddst((int) event.getX(), (int) event.getY())) {
		    touchFlag = true;
		    // mPaint.setColor(Color.RED);
		    return true;
		}
	    }
	}

	if (event.getAction() == MotionEvent.ACTION_MOVE && touchFlag) {
	    // 判断是否点击了哪个个小矩形框
	    if (scaleFlag) {
		if (this.isOutOfAreaScale(event.getX(), event.getY())) {
		    return true;
		}
	    } else {
		if (this.isOutOfArea(event.getX(), event.getY())) {
		    return true;
		}
	    }

	    // 如果选择区域大小跟图像大小一样时，就不能移动
	    if (ChooseArea.left == dst.left && ChooseArea.top == dst.top
		    && ChooseArea.right == dst.right
		    && ChooseArea.bottom == dst.bottom) {
	    } else {
		this.moveChooseArea(event.getX() - mx, event.getY() - my);
		mx = event.getX();
		my = event.getY();
	    }
	}

	// 触笔移出屏幕时，将一些变量设回初值
	if (event.getAction() == MotionEvent.ACTION_UP) {
	    recFlag = -1;
	    this.invalidate();
	    touchFlag = false;
	}

	return super.onTouchEvent(event);
    }

    // 判断是否要超出图片区域，这个函数会调用下面四个press打头的函数
    // 这个函数也会重绘整个画布，也就是选择区域会随着鼠标的移动改变
    private boolean isOutOfArea(float x, float y) {
	float difX = x - mx;
	float difY = y - my;
	switch (recFlag) {
	case Crop_Canvas.PRESS_LB:
	    this.pressLB(difX, difY);
	    break;
	case Crop_Canvas.PRESS_LT:
	    this.pressLT(difX, difY);
	    break;
	case Crop_Canvas.PRESS_RB:
	    this.pressRB(difX, difY);
	    break;
	case Crop_Canvas.PRESS_RT:
	    this.pressRT(difX, difY);
	    break;
	default:
	    return false;
	}
	mx = x;
	my = y;
	this.invalidate();
	return true;
    }

    private boolean isOutOfAreaScale(float x, float y) {

	float difX = x - mx;
	float difY = y - my;
	Log.d("tag", "lastX" + mx + "\t lastY" + my);
	Log.d("tag", "currentX" + x + "\t currentY" + y);
	Log.d("tag", "difX" + difX + "\t difY" + difY);

	switch (recFlag) {
	case Crop_Canvas.PRESS_LB:
	    this.pressLBScale(difX, difY);
	    break;
	case Crop_Canvas.PRESS_LT:
	    this.pressLTScale(difX, difY);
	    break;
	case Crop_Canvas.PRESS_RB:
	    this.pressRBScale(difX, difY);
	    break;
	case Crop_Canvas.PRESS_RT:
	    this.pressRTScale(difX, difY);
	    break;
	default:
	    return false;
	}
	mx = x;
	my = y;
	this.invalidate();
	return true;
    }

    // 找到点击了哪个矩形框（改变选择区域大小的小矩形框）
    // 这个是在MotionEvent.ACTION_DOWN这个动作时执行的
    // 是为了在MotionEvent.ACTION_MOVE的时候，知道应该移动哪个小矩形框
    public boolean findPresseddst(int x, int y) {
	boolean returnFlag = false;
	if (this.isInRect(x, y, recLB)) {
	    recFlag = Crop_Canvas.PRESS_LB;
	    returnFlag = true;
	} else if (this.isInRect(x, y, recLT)) {
	    recFlag = Crop_Canvas.PRESS_LT;
	    returnFlag = true;
	} else if (this.isInRect(x, y, recRB)) {
	    recFlag = Crop_Canvas.PRESS_RB;
	    returnFlag = true;
	} else if (this.isInRect(x, y, recRT)) {
	    recFlag = Crop_Canvas.PRESS_RT;
	    returnFlag = true;
	}

	return returnFlag;
    }

    public boolean isInRect(int x, int y, RectF rect) {
	if (x >= rect.left - 20 && x <= rect.right + 20 && y > rect.top - 20
		&& y < rect.bottom + 20) {
	    return true;
	}
	return false;
    }

    // 点击角落矩形框改变选择区域大小时，不能超过图片所在的区域
    // 下面以press打头的四个函数就是判断是否超出图片区域
    // 如果超出了，就移动不了
    // 不超出按照触笔移动的距离来移动小矩形框

    // pressLB是当点击左下角小矩形框改变大小时是否超出图片区域
    // 如果超出就将left与bottom的值设为图片区域的left和bottom
    private void pressLB(float x, float y) {

	float left = ChooseArea.left + x;
	float right = ChooseArea.right;
	float top = ChooseArea.top;
	float bottom = ChooseArea.bottom + y;
	// Log.d("tag", "选择区域的宽" + Math.abs(ChooseArea.left -
	// ChooseArea.right));
	// Log.d("tag", "选择区域的高" + Math.abs(ChooseArea.top -
	// ChooseArea.bottom));
	if (left <= right - 30 && left >= dst.left && bottom <= dst.bottom
		&& bottom >= top + 30) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (left + x < dst.left) {
		left = dst.left;
	    }

	    if (bottom + y > dst.bottom) {
		bottom = dst.bottom;
	    }

	    if (ChooseArea.left + x > ChooseArea.right - 30) {
		left = ChooseArea.right - 30;
	    }

	    if (ChooseArea.bottom + y < ChooseArea.top + 30) {
		bottom = ChooseArea.top + 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    private void pressLBScale(float x, float y) {
	if (x == 0 || y == 0 || Math.signum(x) == Math.signum(y)) {
	    return;
	}
	float f = Math.max(Math.abs(x), Math.abs(y));
	Log.d("tag", " 取最大值" + f);

	float left = ChooseArea.left + f * Math.signum(x);
	float right = ChooseArea.right;
	float top = ChooseArea.top;
	float bottom = ChooseArea.bottom + f * Math.signum(y);
	if (left < 0 || bottom > dst.bottom) {
	    return;
	}
	Log.d("tag", "left " + left + "\t right " + right + " \t top " + top
		+ "\t bottom " + bottom);
	Log.d("tag",
		"pressLBScale宽" + Math.abs(ChooseArea.left - ChooseArea.right));
	Log.d("tag",
		"pressLBScale高" + Math.abs(ChooseArea.top - ChooseArea.bottom));
	if (left <= right - 30 && left >= dst.left && bottom <= dst.bottom
		&& bottom >= top + 30) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (left + x < dst.left) {
		left = dst.left;
	    }

	    if (bottom + y > dst.bottom) {
		bottom = dst.bottom;
	    }

	    if (ChooseArea.left + x > ChooseArea.right - 30) {
		left = ChooseArea.right - 30;
	    }

	    if (ChooseArea.bottom + y < ChooseArea.top + 30) {
		bottom = ChooseArea.top + 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	// this.setPressRecLoc();
    }

    /**
     * 
     * 描述： pressLT是当点击左上角小矩形框改变大小时是否超出图片区域如果超出就将left与top的值设为图片区域的left和top
     * 
     * @param x
     * @param y
     * @throws
     * @date：2013-9-25 下午3:45:40
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    private void pressLT(float x, float y) {
	float left = ChooseArea.left + x;
	float right = ChooseArea.right;
	float top = ChooseArea.top + y;
	float bottom = ChooseArea.bottom;
	if (left <= right - 30 && left >= dst.left && top <= bottom - 30
		&& top >= dst.top) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (left < dst.left) {
		left = dst.left;
	    }

	    if (top < dst.top) {
		top = dst.top;
	    }

	    if (left > right - 30) {
		left = right - 30;
	    }

	    if (top > bottom - 30) {
		top = bottom - 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    private void pressLTScale(float x, float y) {
	if (x == 0 || y == 0 || Math.signum(x) != Math.signum(y)) {
	    return;
	}
	float f = Math.max(Math.abs(x), Math.abs(y));
	float left = ChooseArea.left + f * Math.signum(x);
	float right = ChooseArea.right;
	float top = ChooseArea.top + f * Math.signum(y);
	float bottom = ChooseArea.bottom;

	if (top < 0 || left < 0) {
	    return;
	}
	Log.d("tag", "left " + left + "\t right " + right + " \t top " + top
		+ "\t bottom " + bottom);
	Log.d("tag",
		"pressLBScale宽" + Math.abs(ChooseArea.left - ChooseArea.right));
	Log.d("tag",
		"pressLBScale高" + Math.abs(ChooseArea.top - ChooseArea.bottom));
	if (left <= right - 30 && left >= dst.left && top <= bottom - 30
		&& top >= dst.top) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (left < dst.left) {
		left = dst.left;
	    }

	    if (top < dst.top) {
		top = dst.top;
	    }

	    if (left > right - 30) {
		left = right - 30;
	    }

	    if (top > bottom - 30) {
		top = bottom - 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    /**
     * 
     * 描述：pressRT是当点击右上角小矩形框改变大小时是否超出图片区域如果超出就将right与top的值设为图片区域的right和top
     * 
     * @param x
     * @param y
     * @throws
     * @date：2013-9-25 下午3:55:59
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    private void pressRT(float x, float y) {

	float left = ChooseArea.left;
	float right = ChooseArea.right + x;
	float top = ChooseArea.top + y;
	float bottom = ChooseArea.bottom;

	if (right <= dst.right && right >= left + 30 && top <= bottom - 30
		&& top >= dst.top) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (right > dst.right) {
		right = dst.right;
	    }

	    if (top < dst.top) {
		top = dst.top;
	    }

	    if (right < left + 30) {
		right = left + 30;
	    }

	    if (top > bottom - 30) {
		top = bottom - 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    private void pressRTScale(float x, float y) {
	if (x == 0 || y == 0 || Math.signum(x) == Math.signum(y)) {
	    return;
	}
	float f = Math.max(Math.abs(x), Math.abs(y));
	float left = ChooseArea.left;
	float right = ChooseArea.right + f * Math.signum(x);
	float top = ChooseArea.top + f * Math.signum(y);
	float bottom = ChooseArea.bottom;
	if (top < 0 || right > dst.right) {
	    return;
	}
	if (right <= dst.right && right >= left + 30 && top <= bottom - 30
		&& top >= dst.top) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (right > dst.right) {
		right = dst.right;
	    }

	    if (top < dst.top) {
		top = dst.top;
	    }

	    if (right < left + 30) {
		right = left + 30;
	    }

	    if (top > bottom - 30) {
		top = bottom - 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    // pressRB是当点击右下角小矩形框改变大小时是否超出图片区域
    // 如果超出就将right与bottom的值设为图片区域的right和bottom
    private void pressRB(float x, float y) {
	float left = ChooseArea.left;
	float right = ChooseArea.right + x;
	float top = ChooseArea.top;
	float bottom = ChooseArea.bottom + y;

	if (right <= dst.right && right >= left + 30 && bottom <= dst.bottom
		&& bottom >= top + 30) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (right > dst.right) {
		right = dst.right;
	    }

	    if (bottom > dst.bottom) {
		bottom = dst.bottom;
	    }

	    if (right < left + 30) {
		right = left + 30;
	    }

	    if (bottom < top + 30) {
		bottom = top + 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    private void pressRBScale(float x, float y) {
	Log.d("tag", "x " + x + "   y " + y);
	if (x == 0 || y == 0 || Math.signum(x) != Math.signum(y)) {
	    return;
	}
	float f = Math.max(Math.abs(x), Math.abs(y));
	float right = ChooseArea.right + f * Math.signum(x);
	float top = ChooseArea.top;
	float left = ChooseArea.left;
	float bottom = ChooseArea.bottom + f * Math.signum(y);
	Log.d("tag", "选择区域的宽" + Math.abs(ChooseArea.left - ChooseArea.right));
	Log.d("tag", "选择区域的高" + Math.abs(ChooseArea.top - ChooseArea.bottom));
	if (right > dst.right || bottom > dst.bottom) {
	    return;
	}
	if (right <= dst.right && right >= left + 30 && bottom <= dst.bottom
		&& bottom >= top + 30) {
	    ChooseArea.set(left, top, right, bottom);
	} else {
	    if (right > dst.right) {
		right = dst.right;
	    }

	    if (bottom > dst.bottom) {
		bottom = dst.bottom;
	    }

	    if (right < left + 30) {
		right = left + 30;
	    }

	    if (bottom < top + 30) {
		bottom = top + 30;
	    }
	    ChooseArea.set(left, top, right, bottom);
	}
	this.setPressRecLoc();
    }

    /**
     * 每次改变选择区域矩形的大小或者移动，各角落上的小矩形也要改变它的Location 描述：
     * 
     * @throws
     * @date：2013-9-16 下午2:31:17
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    private void setPressRecLoc() {
	recLT.set(ChooseArea.left - 10, ChooseArea.top - 10,
		ChooseArea.left + 10, ChooseArea.top + 10);
	recLB.set(ChooseArea.left - 5, ChooseArea.bottom - 5,
		ChooseArea.left + 5, ChooseArea.bottom + 5);
	recRT.set(ChooseArea.right - 5, ChooseArea.top - 5,
		ChooseArea.right + 5, ChooseArea.top + 5);
	recRB.set(ChooseArea.right - 5, ChooseArea.bottom - 5,
		ChooseArea.right + 5, ChooseArea.bottom + 5);
    }

    /**
     * 判断触笔是否在选择区域内 描述：
     * 
     * @param x
     * @param y
     * @return
     * @throws
     * @date：2013-9-17 上午10:53:42
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public boolean judgeLocation(float x, float y) {
	float start_x = this.getChooseArea().left;
	float start_y = this.getChooseArea().top;
	float last_x = this.getChooseArea().right;
	float last_y = this.getChooseArea().bottom;
	// System.out.println("chubi:" + x + "," + y);
	// System.out.println(start_y + "," + last_y);
	if (x > start_x + 10 && x < last_x - 10 && y > start_y + 10
		&& y < last_y - 10) {
	    return true;
	}
	return false;
    }

    public void onDraw(Canvas canvas) {
	super.onDraw(canvas);
	// Log.d("tag", "onDraw");
	// set_LeftArea_Alpha();
	canvas.drawRect(ChooseArea, mPaint);
	// canvas.drawRect(recLT, rectPaint);
	// canvas.drawRect(recLB, rectPaint);
	// canvas.drawRect(recRT, rectPaint);
	// canvas.drawRect(recRB, rectPaint);
	canvas.drawCircle(ChooseArea.left, ChooseArea.top, 10f, rectPaint);
	canvas.drawCircle(ChooseArea.left, ChooseArea.bottom, 10f, rectPaint);
	canvas.drawCircle(ChooseArea.right, ChooseArea.top, 10f, rectPaint);
	canvas.drawCircle(ChooseArea.right, ChooseArea.bottom, 10f, rectPaint);
	// canvas.drawRect(leftRectL, leftAreaPaint);
	// canvas.drawRect(leftRectR, leftAreaPaint);
	// canvas.drawRect(leftRectT, leftAreaPaint);
	// canvas.drawRect(leftRectB, leftAreaPaint);

    }

    public void set_LeftArea_Alpha() {
	leftRectL.set(dst.left, dst.top, ChooseArea.left, dst.bottom);
	leftRectR.set(ChooseArea.right, dst.top, dst.right, dst.bottom);
	leftRectT.set(ChooseArea.left, dst.top, ChooseArea.right,
		ChooseArea.top);
	leftRectB.set(ChooseArea.left, ChooseArea.bottom, ChooseArea.right,
		dst.bottom);
    }

    /**
     * 
     * 描述：是否等比例缩放
     * 
     * @param flag
     * @throws
     * @date：2013-9-25 下午5:02:13
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setScale(boolean flag) {
	scaleFlag = flag;
    }

    /**
     * 
     * 描述： 给image拍照
     * 
     * @return
     * @throws
     * @date：2013-9-30 下午4:17:57
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public Bitmap convertViewToBitmap() {
	measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
		MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
	layout(getLeft(), getTop(), getMeasuredWidth(), getMeasuredHeight());
	setDrawingCacheEnabled(true);
	buildDrawingCache();
	Bitmap bitmap = getDrawingCache();
	// setDrawingCacheEnabled(false);
	return bitmap;
    }

    public Bitmap convertViewToBitmap(View view) {
	view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
		MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
	view.layout(view.getLeft(), view.getTop(), view.getMeasuredWidth(),
		view.getMeasuredHeight());
	Log.d("tag", "getMeasuredWidth  " + view.getMeasuredWidth());
	Log.d("tag", "getMeasuredHeight " + view.getMeasuredHeight());
	view.buildDrawingCache();
	Bitmap bitmap = view.getDrawingCache();
	return bitmap;
    }
}